package com.gzhryc.shared_device.oem.code.place.services;

import java.sql.SQLException;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.code.place.dao.PlaceLogsRecordDao;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceLogsRecord;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceProfitUser;
import com.gzhryc.shared_device.oem.code.place.dao.enums.PlaceLogsRecordEnum;
import com.gzhryc.shared_device.oem.code.user.ERoleType;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.code.user.services.UserService;

public class PlaceLogsRecordService {

	static Logger log = Logger.getLogger(PlaceLogsRecordService.class);

    public static PlaceLogsRecordService self() {
        return MultiDBTools.getService(PlaceLogsRecordService.class, true);
    }

    public static PlaceLogsRecordService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, PlaceLogsRecordService.class, true);
    }

    PlaceLogsRecordDao dao;

    public PlaceLogsRecordService(String jdbcKey) {
        this.dao = new PlaceLogsRecordDao(jdbcKey);
    }
    
    public boolean byCreate(PlacePhysicalStore place, OperateInfo operateInfo) {
    	PlaceLogsRecord logsRecord = new PlaceLogsRecord();
    	logsRecord.setPlaceId(place.getId());
    	logsRecord.setType(PlaceLogsRecordEnum.Type.Info.index());
    	logsRecord.setOperatorType(operateInfo.getOperatorType());
    	logsRecord.setOperator(operateInfo.getOperator());
    	logsRecord.setOperatorId(operateInfo.getOperatorId());
    	logsRecord.setContent("创建网点");
    	logsRecord.setCreateDate(place.getCreateDate());
    	
    	try {
			return dao.insert(logsRecord) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
    	return false;
    }
    
    public boolean byUserAdd(PlacePhysicalStore place,PlaceProfitUser placeProfitUser,OperateInfo operateInfo) {
    	User user = placeProfitUser.getUser();
    	if(user == null) {
    		user = UserService.self().getById(placeProfitUser.getUserId());
    	}
    	
    	if(user != null) {
	    	PlaceLogsRecord logsRecord = new PlaceLogsRecord();
	    	logsRecord.setPlaceId(place.getId());
	    	logsRecord.setType(PlaceLogsRecordEnum.Type.User.index());
	    	logsRecord.setOperatorType(operateInfo.getOperatorType());
	    	logsRecord.setOperator(operateInfo.getOperator());
	    	logsRecord.setOperatorId(operateInfo.getOperatorId());
	    	
	    	String content =  "添加【" + placeProfitUser.getRoleTypeName() + "】" + user.getFullName();
	    	if(StringTools.isBlank(placeProfitUser.getIncomeRatioContent())) {
	    		if(ERoleType.Partner.index().equals(placeProfitUser.getRoleType())) {
	    			content = content  + ",默认分成比例";
	    		}else {
	    			content = content  + ",未设置分成比例";
	    		}
	    	}else {
	    		content = content + ",分成比例：" + placeProfitUser.getIncomeRatioContent();
	    	}
	    	logsRecord.setContent(content);
	    	logsRecord.setCreateDate(place.getCreateDate());
	    	
	    	try {
				return dao.insert(logsRecord) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}else {
    		log.error("记录网点日志失败，未找到新增用户信息，网点ID：{{0}}，用户ID：{{1}}", placeProfitUser.getPlaceId(),placeProfitUser.getUserId());
    	}
    	return false;
    }
}
